# -*- coding: utf-8 -*-
import re

from flask import request, jsonify, session, current_app

from ihome import redis_store, db, constants
from ihome.api_1_0 import api
from ihome.models import User
from ihome.utils.response_code import RET


@api.route('/users', methods=['POST'])
def register():
    # 接受参数，手机号，验证码
    # get_json方法能帮助请求体的json数据转换为字典
    req_dict = request.get_json()
    mobile = req_dict.get("mobile")
    sms_code = req_dict.get("sms_code")
    password = req_dict.get("password")
    if not all([mobile, sms_code, password]):
        resp = {
            'errno': RET.PARAMERR,
            'errmsg': '参数不完整'
        }
        return jsonify(resp)
    # 业务逻辑
    # 获取真实的短信验证码
    try:
        real_sms_code = redis_store.get('sms_code_%s' % mobile)
    except Exception as e:
        current_app.logger.error(e)
        resp = {
            'errno': RET.DBERR,
            'errmsg': '查询短信验证码错误'
        }
        return jsonify(resp)
    # 判断短信验证码是否过期
    print('=----%s' % real_sms_code)
    print('=====%s' % sms_code)
    if real_sms_code is None:
        resp = {
            'errno': RET.NODATA,
            'errmsg': '短信验证码过期'
        }
        return jsonify(resp)
    # 对于用户输入的短信验证码是否正确
    if real_sms_code != sms_code:
        resp = {
            'errno': RET.DATAERR,
            'errmsg': '短信验证码错误'
        }
        return jsonify(resp)
    # 判断手机号是否正确
    if not re.match(r'1[34578]\d{9}', mobile):
        resp = {
            'errno': RET.DATAERR,
            'errmsg': '手机号格式错误'
        }
        return jsonify(resp)
    # 删除短信验证码
    try:
        redis_store.delete('sms_code_%s' % mobile)
    except Exception as e:
        current_app.logger.error(e)
    # #  判断手机号是否注册
    # try:
    #     user = User.query.filter_by(mobile=mobile).first()
    # except Exception as e:
    #     current_app.logger.error(e)
    #     resp={
    #         'errno':RET.DBERR,
    #         'errmsg':'数据库异常'
    #     }
    #     return jsonify(resp)
    #
    # if user is not None:
    #     resp ={
    #         'errno':RET.DATAEXIST,
    #         'errmsg':'用户手机号已经注销'
    #     }
    #     return jsonify(resp)
    # 保存用户的数据到数据库中
    user = User(name=mobile, mobile=mobile)
    user.password = password
    try:
        db.session.add(user)
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        db.session.rollback()
        # 表示注册过
        resp = {
            'errno': RET.DATAEXIST,
            'errmsg': '用户手机号已经注册'
        }
        return jsonify(resp)
    # 利用 session记录用户的登录状态
    session['user_id'] = user.id
    session['user_name'] = user.name
    session['mobile'] = user.mobile
    # 返回值
    resp = {
        'errno': RET.OK,
        'errmsg': '注册成功'
    }
    return jsonify(resp)


# 用户登陆
@api.route('/session', methods=['POST'])
def login():
    # 获取用户密码
    req_dict = request.get_json()
    mobile = req_dict.get('mobile')
    password = req_dict.get('password')
    # 检验参数
    if not all([mobile, password]):
        rep = {
            'errno': RET.PARAMERR,
            'errmsg': '参数错误'
        }
        return jsonify(rep)
    # 校验参数
    # 手机号码校验
    if not re.match(r'1[34578]\d{9}$', mobile):
        rep = {
            'errno': RET.PARAMERR,
            'errmsg': '手机格式错误'
        }
        return jsonify(rep)
    # 判断用户的错误次数
    # 从redis中获取错误次数
    user_ip = request.remote_addr
    try:
        access_counts = redis_store.get('access_%s' % user_ip)
    except Exception as e:
        current_app.logger.error(e)
    else:
        if access_counts is not None and int(access_counts) >= constants.LOGIN_ERROR_MAX_NUM:
            return jsonify(errno=RET.REQERR, errmsg='登录过于频繁')
    # 查询数据库，判断用户信息与密码
    try:
        user = User.query.filter_by(mobile=mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询用户信息异常')
    # 用user模型中实现检验用户密码
    if user is None or not user.check_password(password):
        try:
            redis_store.incr('access_%s' % user_ip)
            redis_store.expire("access_%s" % user_ip, constants.LOGIN_ERROR_FORBID_TIME)
        except Exception as e:
            current_app.logger.error(e)
        rep = {
            'errno': RET.LOGINERR,
            'errmsg': '用户名或密码输入错误'
        }
        return jsonify(rep)
    # 登录成功
    # 清除用户的登录错误次数
    try:
        redis_store.delete('access_%s' % user_ip)
    except Exception as e:
        current_app.logger.error(e)
    # 用户验证成功 session记录用户的登录状态
    session['user_id'] = user.id
    session['user_name'] = user.name
    session['mobile'] = user.mobile
    return jsonify(errno=RET.OK, errmsg='登录成功', data={'user_id': user.id})


@api.route('/session', methods=['GET'])
def check_login():
    # 尝试从session中获取用户的名字
    name = session.get('user_name')
    # 如果session中数据name名字存在，则表示用户已登录，否则未登录
    if name is not None:
        return jsonify(errno=RET.OK, errmsg='true', data={'name': name})
    else:
        return jsonify(errno=RET.SESSIONERR, errmsg='false')


@api.route('/session', methods=['DELETE'])
def logout():
    # 清除session数据
    session.clear()
    return jsonify(errno=RET.OK, errmsg='OK')
